<?php
/**
 * Created by PhpStorm.
 * User: jocky
 * Date: 2017/6/1
 * Time: 上午11:35
 */
namespace common\controllers;

use yii\filters\AccessControl;
use yii\web\Controller;

class BaseControllers extends Controller
{
    /**
     * Access control
     *
     * @return array
     */
    public function behaviors()
    {
        $rules = [];
        if (!empty(\Yii::$app->params['without_login_controllers'])) {
            $rules[] = [
                'allow' => true,
                'controllers' => \Yii::$app->params['without_login_controllers'],
                'roles' => ['?'],
            ];
        }
        $rules[] = [
            'allow' => true,
            'roles' => ['@'],
        ];

        return [
            'access' => [
                'class' => AccessControl::class,
                'rules' => $rules,
                'denyCallback' => function ($rule, $action) {
                    if (\Yii::$app->request->isAjax) {
                        exit(json_encode([101, '请登录系统', ['url' => \Yii::$app->user->loginUrl]]));
                    } else {
                        \Yii::$app->user->loginRequired();
                        return false;
                    }
                }
            ]
        ];
    }
}
